{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  XGBOOST商品分类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一、导入必备工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import log_loss\n",
    "\n",
    "from xgboost import XGBClassifier\n",
    "import xgboost as xgb\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import StratifiedKFold\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一、读入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 74659 entries, 0 to 74658\n",
      "Columns: 227 entries, bathrooms to work\n",
      "dtypes: float64(9), int64(218)\n",
      "memory usage: 129.3 MB\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 49352 entries, 0 to 49351\n",
      "Columns: 228 entries, bathrooms to interest_level\n",
      "dtypes: float64(9), int64(219)\n",
      "memory usage: 85.8 MB\n",
      "   bathrooms  bedrooms  price  price_bathrooms  price_bedrooms  room_diff  \\\n",
      "0        1.0         1   2950      1475.000000     1475.000000        0.0   \n",
      "1        1.0         2   2850      1425.000000      950.000000       -1.0   \n",
      "2        1.0         1   3758      1879.000000     1879.000000        0.0   \n",
      "3        1.0         2   3300      1650.000000     1100.000000       -1.0   \n",
      "4        2.0         2   4900      1633.333333     1633.333333        0.0   \n",
      "\n",
      "   room_num  Year  Month  Day  ...   virtual  walk  walls  war  washer  water  \\\n",
      "0       2.0  2016      6   11  ...         0     0      0    0       0      0   \n",
      "1       3.0  2016      6   24  ...         0     0      0    1       0      0   \n",
      "2       2.0  2016      6    3  ...         0     0      0    0       0      0   \n",
      "3       3.0  2016      6   11  ...         0     0      0    0       0      0   \n",
      "4       4.0  2016      4   12  ...         0     0      0    1       0      0   \n",
      "\n",
      "   wheelchair  wifi  windows  work  \n",
      "0           0     0        0     0  \n",
      "1           0     0        0     0  \n",
      "2           0     0        0     0  \n",
      "3           1     0        0     0  \n",
      "4           0     0        0     0  \n",
      "\n",
      "[5 rows x 227 columns]\n",
      "data_test  describe:           bathrooms      bedrooms         price  price_bathrooms  \\\n",
      "count  74659.000000  74659.000000  7.465900e+04     74659.000000   \n",
      "mean       1.212915      1.544663  3.749033e+03      1658.561183   \n",
      "std        0.649820      1.107014  9.713092e+03      4771.933806   \n",
      "min        0.000000      0.000000  1.000000e+00         0.500000   \n",
      "25%        1.000000      1.000000  2.495000e+03      1220.000000   \n",
      "50%        1.000000      1.000000  3.150000e+03      1500.000000   \n",
      "75%        1.000000      2.000000  4.100000e+03      1850.000000   \n",
      "max      112.000000      7.000000  1.675000e+06    837500.000000   \n",
      "\n",
      "       price_bedrooms     room_diff      room_num     Year         Month  \\\n",
      "count    74659.000000  74659.000000  74659.000000  74659.0  74659.000000   \n",
      "mean      1631.330597     -0.331748      2.757578   2016.0      5.015738   \n",
      "std       4482.208640      1.026154      1.497497      0.0      0.825815   \n",
      "min          0.333333     -6.000000      0.000000   2016.0      4.000000   \n",
      "25%       1065.000000     -1.000000      2.000000   2016.0      4.000000   \n",
      "50%       1377.500000      0.000000      2.000000   2016.0      5.000000   \n",
      "75%       1950.000000      0.000000      4.000000   2016.0      6.000000   \n",
      "max     558333.333333    109.000000    115.000000   2016.0      6.000000   \n",
      "\n",
      "                Day      ...            virtual          walk         walls  \\\n",
      "count  74659.000000      ...       74659.000000  74659.000000  74659.000000   \n",
      "mean      15.151623      ...           0.001058      0.003094      0.000442   \n",
      "std        8.245418      ...           0.032922      0.055539      0.021020   \n",
      "min        1.000000      ...           0.000000      0.000000      0.000000   \n",
      "25%        8.000000      ...           0.000000      0.000000      0.000000   \n",
      "50%       15.000000      ...           0.000000      0.000000      0.000000   \n",
      "75%       22.000000      ...           0.000000      0.000000      0.000000   \n",
      "max       31.000000      ...           2.000000      1.000000      1.000000   \n",
      "\n",
      "                war        washer         water    wheelchair          wifi  \\\n",
      "count  74659.000000  74659.000000  74659.000000  74659.000000  74659.000000   \n",
      "mean       0.188243      0.008653      0.000388      0.027994      0.002156   \n",
      "std        0.390908      0.097548      0.019705      0.164957      0.046388   \n",
      "min        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "25%        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "50%        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "75%        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "max        1.000000      2.000000      1.000000      1.000000      1.000000   \n",
      "\n",
      "            windows          work  \n",
      "count  74659.000000  74659.000000  \n",
      "mean       0.001085      0.000884  \n",
      "std        0.032921      0.029720  \n",
      "min        0.000000      0.000000  \n",
      "25%        0.000000      0.000000  \n",
      "50%        0.000000      0.000000  \n",
      "75%        0.000000      0.000000  \n",
      "max        1.000000      1.000000  \n",
      "\n",
      "[8 rows x 227 columns]\n",
      "data_train  describe:          bathrooms      bedrooms         price  price_bathrooms  \\\n",
      "count  49352.00000  49352.000000  4.935200e+04     4.935200e+04   \n",
      "mean       1.21218      1.541640  3.830174e+03     1.697863e+03   \n",
      "std        0.50142      1.115018  2.206687e+04     1.100477e+04   \n",
      "min        0.00000      0.000000  4.300000e+01     2.150000e+01   \n",
      "25%        1.00000      1.000000  2.500000e+03     1.225000e+03   \n",
      "50%        1.00000      1.000000  3.150000e+03     1.500000e+03   \n",
      "75%        1.00000      2.000000  4.100000e+03     1.850000e+03   \n",
      "max       10.00000      8.000000  4.490000e+06     2.245000e+06   \n",
      "\n",
      "       price_bedrooms     room_diff      room_num     Year         Month  \\\n",
      "count    4.935200e+04  49352.000000  49352.000000  49352.0  49352.000000   \n",
      "mean     1.657567e+03     -0.329460      2.753820   2016.0      5.014852   \n",
      "std      7.817996e+03      0.947732      1.446091      0.0      0.824442   \n",
      "min      4.300000e+01     -5.000000      0.000000   2016.0      4.000000   \n",
      "25%      1.066667e+03     -1.000000      2.000000   2016.0      4.000000   \n",
      "50%      1.383417e+03      0.000000      2.000000   2016.0      5.000000   \n",
      "75%      1.962500e+03      0.000000      4.000000   2016.0      6.000000   \n",
      "max      1.496667e+06      8.000000     13.500000   2016.0      6.000000   \n",
      "\n",
      "                Day       ...                walk         walls           war  \\\n",
      "count  49352.000000       ...        49352.000000  49352.000000  49352.000000   \n",
      "mean      15.206881       ...            0.003080      0.000385      0.186477   \n",
      "std        8.280749       ...            0.055412      0.019618      0.389495   \n",
      "min        1.000000       ...            0.000000      0.000000      0.000000   \n",
      "25%        8.000000       ...            0.000000      0.000000      0.000000   \n",
      "50%       15.000000       ...            0.000000      0.000000      0.000000   \n",
      "75%       22.000000       ...            0.000000      0.000000      0.000000   \n",
      "max       31.000000       ...            1.000000      1.000000      1.000000   \n",
      "\n",
      "             washer         water    wheelchair          wifi       windows  \\\n",
      "count  49352.000000  49352.000000  49352.000000  49352.000000  49352.000000   \n",
      "mean       0.009361      0.000446      0.028165      0.002026      0.001013   \n",
      "std        0.101625      0.021109      0.165446      0.044969      0.031814   \n",
      "min        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "25%        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "50%        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "75%        0.000000      0.000000      0.000000      0.000000      0.000000   \n",
      "max        2.000000      1.000000      1.000000      1.000000      1.000000   \n",
      "\n",
      "               work  interest_level  \n",
      "count  49352.000000    49352.000000  \n",
      "mean       0.000952        1.616895  \n",
      "std        0.030846        0.626035  \n",
      "min        0.000000        0.000000  \n",
      "25%        0.000000        1.000000  \n",
      "50%        0.000000        2.000000  \n",
      "75%        0.000000        2.000000  \n",
      "max        1.000000        2.000000  \n",
      "\n",
      "[8 rows x 228 columns]\n"
     ]
    }
   ],
   "source": [
    "data_test = pd.read_csv('./data/'+\"RentListingInquries_FE_test.csv\")\n",
    "data_train = pd.read_csv('./data/'+\"RentListingInquries_FE_train.csv\")\n",
    "data_test.info()\n",
    "data_train.info()\n",
    "print(data_test.head(5))\n",
    "data_train.head(5)\n",
    "\n",
    "print(\"data_test  describe:\",data_test.describe())\n",
    "print(\"data_train  describe:\",data_train.describe())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  二、确定弱分类器的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳弱分类器个数： 281\n"
     ]
    }
   ],
   "source": [
    "y_train = data_train['interest_level']\n",
    "x_train = data_train.drop('interest_level',axis = 1)\n",
    "\n",
    "#数据分层器\n",
    "kfold = StratifiedKFold(n_splits=5,shuffle=True, random_state=3)\n",
    "\n",
    "#使用xgboost的cv交叉验证进行最优结果选取\n",
    "def modelfit(alg, x_train, y_train, cv_folds=None, early_stopping_rounds=10):\n",
    "    xgb_param=alg.get_xgb_params()\n",
    "    xgb_param['num_class']=150\n",
    "    \n",
    "    xgbtrain = xgb.DMatrix(x_train, label=y_train)\n",
    "    cvresult = xgb.cv(xgb_param, xgbtrain, num_boost_round=alg.get_params()['n_estimators'], folds=list(cv_folds.split(x_train,y_train)),\n",
    "                     metrics='mlogloss', early_stopping_rounds=early_stopping_rounds)\n",
    "    cvresult.to_csv('nestimators.csv', index_label='n_estimators')\n",
    "\n",
    "    n_estimator=cvresult.shape[0]\n",
    "    \n",
    "    alg.set_params(n_estimators=n_estimator)\n",
    "    alg.fit(x_train, y_train, eval_metric='mlogloss')\n",
    "    print(\"最佳弱分类器个数：\",n_estimator)\n",
    "\n",
    "\n",
    "xgb1 = XGBClassifier(\n",
    "    learning_rate=0.1,\n",
    "    n_estimators=1000,\n",
    "    max_depth=5,\n",
    "    min_child_weight=0.1,\n",
    "    gamma = 0,                #最小损失函数下降值\n",
    "    subsample=0.3,            #每棵树的随机采样比例，比较小时可以避免过拟合\n",
    "    colsample_bytree=0.8,     #每棵树随机采样，列数占比，每列是一个特征值\n",
    "    colsample_bylevel=0.7,    #每一层每一级的裂变，列数采样所占比例\n",
    "    objective='multi:softprob',   #定义最小损失函数  bianry:logistic  multi:softmax\n",
    "    seed=3                    #随机数的种子，复现随机数的结果\n",
    ")\n",
    "\n",
    "modelfit(xgb1, x_train, y_train, cv_folds=kfold)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "通过xgboost获取到弱分类器的个数为281."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
